<?php
/**
 * 读取 抓取配置数据库
 */
header("content-type:text/html;charset=utf8");
require './include/config.php';
function cleartrim($str) {
	$str = str_replace(" ","",$str);
	$str = str_replace("　","",$str);
	return $str;
}
$provinceData = array();
connect_mysql();
$query = mysql_query("SELECT * from sys_province");
$i = 0;
while ($rs = mysql_fetch_array($query)) {
	$cityData="";
	$query2 = mysql_query("SELECT * from sys_city where provincecode = ".$rs["code"]."");
	$k = 0;
	while ($rs2 = mysql_fetch_array($query2)) {
		$areaData="";
		$query3 = mysql_query("SELECT * from sys_area where citycode = ".$rs2["code"]."");
		$j = 0;
		while ($rs3 = mysql_fetch_array($query3)) {
			$areaData[$j] = array(
				"name"=>cleartrim($rs3["name"]),
				"value"=>$rs3["code"]
			);
			$j++;
		}
		$cityData[$k] = array(
			"name"=>cleartrim($rs2["name"]),
			"value"=>$rs2["code"],
			"data"=>$areaData
		);
		$k++;
	}
	$provinceData[$i] = array(
		"name"=>cleartrim($rs["name"]),
		"value"=>$rs["code"],
		"data"=>$cityData
	);
	$i++;
}

$industry = array();
$query = mysql_query("SELECT * from sys_industry where classid < 100");
$i = 0;
while ($rs = mysql_fetch_array($query)) {
	$class="";
	$whereMin=$rs["classid"] * 100;
	$whereMax=$rs["classid"] * 100 + 99;
	$query2 = mysql_query("SELECT * from sys_industry where classid > ".$whereMin." and classid < ".$whereMax."");
	$k = 0;
	while ($rs2 = mysql_fetch_array($query2)) {
		$type="";
		$whereMin=$rs2["classid"] * 1000;
		$whereMax=$rs2["classid"] * 1000 + 999;
		$query3 = mysql_query("SELECT * from sys_industry where classid > ".$whereMin." and classid < ".$whereMax."");
		$j = 0;
		while ($rs3 = mysql_fetch_array($query3)) {
			$type[$j] = array(
				"name"=>cleartrim($rs3["name"]),
				"value"=>$rs3["classid"]
			);
			$j++;
		}
		$class[$k] = array(
			"name"=>cleartrim($rs2["name"]),
			"value"=>$rs2["classid"],
			"data"=>$type
		);
		$k++;
	}
	$industry[$i] = array(
		"name"=>cleartrim($rs["name"]),
		"value"=>$rs["classid"],
		"data"=>$class
	);
	$i++;
}
$json = json_encode(array("province"=>$provinceData,"industry"=>$industry));
$counter_file = 'data.txt';
$fopen = fopen($counter_file, 'wb');
fputs($fopen,$json);
fclose($fopen);
echo "数据准备完毕，开始愉快的<a href=\"index.php\">采集</a>吧。";
?>